package com.frinder.data.domain.web;

import com.frinder.base.global.Result;
import com.frinder.data.base.PageWrapper;
import com.frinder.data.domain.model.User;
import com.frinder.data.domain.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody @Valid User user) {
        userService.save(user);
        return Result.defSuccess();
    }

    @ApiOperation("删除用户")
    @PostMapping("/delete")
    public Result delete(@RequestParam String id) {
        userService.delete(id);
        return Result.defSuccess();
    }

    @ApiOperation("更新用户")
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        userService.update(user.getId(), user);
        return Result.defSuccess();
    }

    @ApiOperation("所有用户")
    @PostMapping("/list")
    public Result list() {
        return Result.dataSuccess(userService.listAll());
    }

    @ApiOperation("根据Sid查找用户")
    @PostMapping("/findBySid")
    public Result findBySid(@RequestParam String sid) {
        return Result.dataSuccess(userService.findBySid(sid));
    }

    @ApiOperation("根据Id查找用户")
    @PostMapping("/findById")
    public Result findById(@RequestParam String id) {
        return Result.dataSuccess(userService.findById(id));
    }

    @ApiOperation("分页方法")
    @PostMapping("/page")
    public Result page(@RequestBody PageWrapper<User> pageWrapper) {
        return Result.dataSuccess(userService.page(pageWrapper));
    }

}
